local skel = fk.CreateSkill {
  name = "emo__jisheng",
}

Fk:loadTranslationTable{
  ["emo__jisheng"] = "籍生",
  [":emo__jisheng"] = "出牌阶段各限一次，你可以①将一张牌当【借刀杀人】使用并交给目标；②重铸所有手牌并移动场上一张牌。",

  ["#emo__jisheng"] = "籍生：选一项发动",
  ["emo__jisheng_jiedao"] = "借刀并交给",
  ["emo__jisheng_recast"] = "重铸并移动",
  ["#emo__jisheng-move"] = "籍生:移动场上一张牌",

  ["$emo__jisheng1"] = "傅佥不明而愚忠，蒋舒已知天命归魏",
  ["$emo__jisheng2"] = "其势摇摆，暂缓做决策，且待时机稳固。",
}

local all_choices = {"emo__jisheng_jiedao", "emo__jisheng_recast"}

---@param player Player
local getChoices = function (player)
  local choices = {}
  if player:getMark("emo__jisheng_jiedao-turn") == 0 then
    table.insert(choices, "emo__jisheng_jiedao")
  end
  if player:getMark("emo__jisheng_recast-turn") == 0 and not player:isKongcheng() then
    table.insert(choices, "emo__jisheng_recast")
  end
  return choices
end

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#emo__jisheng",
  interaction = function(self, player)
    local choices = getChoices(player)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and self.interaction.data == "emo__jisheng_jiedao" then
      local card = Fk:cloneCard("collateral")
      card.skillName = skel.name
      card:addSubcard(to_select)
      return not player:prohibitUse(card)
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected < 2 and self.interaction.data == "emo__jisheng_jiedao" and #selected_cards == 1 then
      local card = Fk:cloneCard("collateral")
      card.skillName = skel.name
      card:addSubcard(selected_cards[1])
      return card.skill:targetFilter(player, to_select, selected, selected_cards, card)
    end
  end,
  feasible = function (self, player, selected, selected_cards, card)
    if self.interaction.data == "emo__jisheng_jiedao" then
      return #selected == 2 and #selected_cards == 1
    end
    return true
  end,
  can_use = function(self, player)
    return #getChoices(player) > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = self.interaction.data
    if choice == nil then return end
    room:setPlayerMark(player, choice .. "-turn", 1)
    if choice == "emo__jisheng_jiedao" then
      local card = Fk:cloneCard("collateral")
      card.skillName = skel.name
      card:addSubcard(effect.cards[1])
      room:useCard{
        from = player, tos = effect.tos, card = card
      }
    else
      room:recastCard(player:getCardIds("h"), player, skel.name)
      if player.dead or #room:canMoveCardInBoard() == 0 then return end
      local targets = room:askToChooseToMoveCardInBoard(player, {
        skill_name = skel.name, prompt = "#emo__jisheng-move",
      })
      if #targets == 0 then return end
      room:askToMoveCardInBoard(player, {skill_name = skel.name, target_one = targets[1], target_two = targets[2] })
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return table.contains(data.card.skillNames, skel.name) and data.tos and table.contains(data.tos, player)
    and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:obtainCard(player, data.card, false, fk.ReasonGive, data.from, skel.name)
  end,
})

return skel
